package part12;

/**
 * @author wcj
 * @Date 2021/5/21 16:14
 * @Version 1.0
 */
public class Part129 {

    private static Switch aSwitch = new Switch();

    public static void f() throws OnOffException1, OnOffException2 {
    }

    /**
     * finally语句做的工作：
     * 因为有垃圾收集器，所以通常内存的回收并不需要我们自己手动做
     * 我们需要做的就是除了内存之外的其它情况进行一个初始化
     *
     * @param args
     */
    public static void main(String[] args) {
        try {
            aSwitch.on();
            f();
            aSwitch.off();
        } catch (OnOffException1 e) {
            System.out.println("OnOffException1");
        } catch (OnOffException2 e) {
            System.out.println("OnOffException2");
        } finally {
            aSwitch.off();
        }

    }
}

class Switch {
    private boolean state = false;

    public boolean read() {
        return state;
    }

    public void on() {
        state = true;
        System.out.println(this);
    }

    public void off() {
        state = false;
        System.out.println(this);
    }

    @Override
    public String toString() {
        return state ? "on" : "off";
    }
}

class OnOffException1 extends Exception {
}

class OnOffException2 extends Exception {
}
